package com.mall.service.marketing.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.commons.common.CommonPage;

import com.mall.feign.clients.PmsProductCliens;
import com.mall.model.domain.PmsProduct;
import com.mall.model.domain.SmsFlashPromotionProductRelation;
import com.mall.model.vo.LuXiaoZhuVO;
import com.mall.service.marketing.mapper.FlashProductRelationMapper;
import com.mall.service.marketing.service.FlashProductRelationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName FlashProductRelationServiceImpl
 * @Description
 * @Author Young
 * @Date 2023/3/12 10:11
 * @Version 1.0
 */
@Service
public class FlashProductRelationServiceImpl extends ServiceImpl<FlashProductRelationMapper,SmsFlashPromotionProductRelation> implements FlashProductRelationService {

    @Autowired
    private FlashProductRelationMapper flashProductRelationMapper;
    @Autowired
    private PmsProductCliens pmsProductCliens;

//    @Autowired
//    private PmsProductCliens pmsProductCliens;

    /**
     * 批量选择商品添加关联
     * @param sfppr
     */
    @Override
    public void create(List<SmsFlashPromotionProductRelation> sfppr) {
        this.saveBatch(sfppr);
    }

    /**
     * 删除关联
     * @param id
     */
    @Override
    public void delete(Integer id) {
        flashProductRelationMapper.deleteById(id);
    }

    /**
     * 修改关联信息
     * @param id
     * @param sfppr
     */
    @Override
    public void myUpdate(Integer id, SmsFlashPromotionProductRelation sfppr) {
        flashProductRelationMapper.updateById(sfppr);
    }

    /**
     * 获取管理商品促销信息
     * @param id
     * @return
     */
    @Override
    public SmsFlashPromotionProductRelation get(Integer id) {
        SmsFlashPromotionProductRelation sfppr = flashProductRelationMapper.selectById(id);
        return sfppr;
    }

    /**
     * 分页查询不同场次关联及商品信息
     * @param flashPromotionId
     * @param flashPromotionSessionId
     * @param pageNum
     * @param pageSize
     */
    @Override
    public CommonPage myList(Integer flashPromotionId, Integer flashPromotionSessionId, Integer pageNum, Integer pageSize) {
        Page<SmsFlashPromotionProductRelation> ipage = new Page(pageNum,pageSize);
        //先查询商品的关联表,根据flashPromotionId、flashPromotionSessionId
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SmsFlashPromotionProductRelation::getFlashPromotionId,flashPromotionId)
                .eq(SmsFlashPromotionProductRelation::getFlashPromotionSessionId,flashPromotionSessionId);
        flashProductRelationMapper.selectPage(ipage,queryWrapper);

        List<LuXiaoZhuVO> list = new ArrayList<>();
        LuXiaoZhuVO luXiaoZhuVO = new LuXiaoZhuVO();

        //从Page对象中获取records属性中的SmsFlashPromotionProductRelation对象集合
        for (SmsFlashPromotionProductRelation management : ipage.getRecords()) {
            //放入SmsFlashPromotionProductRelation对象
            BeanUtils.copyProperties(management,luXiaoZhuVO);
            //获取SmsFlashPromotionProductRelation对象中productId属性并查询
            Long productId = management.getProductId();
            PmsProduct productInfo = pmsProductCliens.getProductInfo(productId);
            luXiaoZhuVO.setPmsProduct(productInfo);
            list.add(luXiaoZhuVO);
        }
        CommonPage page = CommonPage.getPage(ipage);
        page.setList(list);
        return page;
    }
}
